<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>MMD Parser</title>
    <meta name="description" content="MMD Parser test">
    <script src="./build/mmdparser.js"></script>
    <script>
      var parser = new MMDParser.Parser();

      function load (url, responseType, mimeType, onLoad, onProgress, onError) {
        var request = new XMLHttpRequest();
        request.open('GET', url, true);
        request.addEventListener('load', function (event) {
          var response = event.target.response;
          if (this.status === 200) {
            onLoad(response);
          } else if (this.status === 0) {
            console.warn('HTTP Status 0 received.');
            onLoad(response);
          } else {
            console.warn('HTTP Status ' + this.status + ' received.');
            onError(event);
          }
        }, false);
        if (onProgress !== undefined) request.addEventListener('progress', onProgress, false);
        if (onError !== undefined) request.addEventListener('error', onError, false);
        request.responseType = responseType;
        if (mimeType !== undefined) request.overrideMimeType(mimeType)
        request.send(null);
        console.log('downloading: ' + url);
      }

      function testPmd () {
        console.log('PMD parse test');
        load(
          'https://cdn.rawgit.com/mrdoob/three.js/dev/examples/models/mmd/miku/miku_v2.pmd',
          'arraybuffer',
          undefined,
          function (buffer) {
            var pmd = parser.parsePmd(buffer);
            console.log(pmd);
            testVmd();
          }
        );
      }

      function testVmd () {
        console.log('VMD parse test');
        load(
          'https://cdn.rawgit.com/mrdoob/three.js/dev/examples/models/mmd/vmds/wavefile_v2.vmd',
          'arraybuffer',
          undefined,
          function (buffer) {
            var vmd = parser.parseVmd(buffer);
            console.log(vmd);
            testVpd();
          }
        );
      }

      function testVpd () {
        console.log('VPD parse test');
        load(
          'https://cdn.rawgit.com/mrdoob/three.js/dev/examples/models/mmd/vpds/01.vpd',
          'text',
          'text/plain; charset=shift_jis',
          function (text) {
            var vpd = parser.parseVpd(text);
            console.log(vpd);
          }
        );
      }

      function run() {
        testPmd();
      }
    </script>
  </head>
  <body onload="run()">
    <span>See JavaScript console.</span>
  </body>
</html>
